<?php
/**
 * Created by xiaosong
 * E-mail:4155433@gmail.com
 * Date: 2019/9/25
 * Time: 11:26
 */

namespace App\Admin\Controllers\base;


use App\Common\statement\CommonStatement;
use App\Http\Controllers\Controller;
use App\Models\fund\Rebate;
use App\Models\fund\Recharge;
use App\Models\goods\Order;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class IndexController extends Controller
{
    public function index(Content $content)
    {


        //近30天充值提现数据
        $day = Carbon::now()->diffInDays(Carbon::now()->subDays(30));
//        $rechargeObj = Rebate::select([
//            DB::raw('DATE(created_at) AS date'),
//            DB::raw("SUM(amount) AS total"),
//        ])
//            ->whereBetween('created_at', [Carbon::now()->subDays(30), Carbon::now()])
//            ->groupBy('date')
//            ->where([
//                'money_type' => 1,
//                'status' => 1
//            ])
//            ->orderBy('date', 'DESC')
//            ->get();
//
//        $recharge = $rechargeObj->pluck([],'date')->toArray();
//
//        $cashObj = Order::select([
//            DB::raw('DATE(updated_at) AS datem'),
//            DB::raw("SUM(total_price) AS total")
//        ])
//            ->whereIn('status',[1,2,3])
//            ->whereBetween('updated_at', [Carbon::now()->subDays(30), Carbon::now()])
//            ->groupBy('datem')
//            ->orderBy('datem', 'DESC')
//            ->get();
//
//        $cash = $cashObj->pluck([],'datem')->toArray();
//
//        $rechargeArr = [];
//        //补0不存在的日期
//        for ($i = $day;$i >=0 ; $i--){
//            $date = Carbon::now()->subDays($i)->toDateString();
//
//            if (!isset($rechargeArr[$date])){
//                $rechargeArr[$date] = [
//                    'date' => $date
//                ];
//            }
//
//            if (!isset($recharge[$date])){
//                $rechargeArr[$date]['recharge'] = 0;
//            }else{
//                $rechargeArr[$date]['recharge'] = $recharge[$date]['total'];
//            }
//
//            if (!isset($cash[$date])){
//                $rechargeArr[$date]['cash'] = 0;
//            }else{
//                $rechargeArr[$date]['cash'] = $cash[$date]['total'];
//            }
//
//            $rechargeArr[$date]['profit'] = $rechargeArr[$date]['recharge'] - $rechargeArr[$date]['cash'];
//
//        }
//
//
//        //近12月按月统计
//        $month = Carbon::now()->diffInMonths(Carbon::now()->subMonth(11)->startOfMonth());
//
//        $monthOjb = Rebate::select([
//            DB::raw('MONTH(created_at) AS month'),
//            DB::raw("SUM(amount) AS total"),
//        ])
//            ->where([
//                'money_type' => 1,
//                'status' => 1
//            ])
//            ->whereBetween('created_at', [Carbon::now()->subMonth(11)->startOfMonth(), Carbon::now()])
//            ->groupBy('month')
//            ->orderBy('month', 'DESC')
//            ->get();
//        $months = $monthOjb->pluck([],'month')->toArray();
//
//
//        $orderObj = Order::select([
//            DB::raw('MONTH(created_at) AS month'),
//            DB::raw("SUM(total_price) AS total"),
//        ])
//            ->whereIn('status',[1,2,3])
//            ->whereBetween('created_at', [Carbon::now()->subMonth(11)->startOfMonth(), Carbon::now()])
//            ->groupBy('month')
//            ->orderBy('month', 'DESC')
//            ->get();
//        $orders = $orderObj->pluck([],'month')->toArray();
//
//        $year = [];
//
//        //补0不存在的日期
//        for ($i = $month;$i >= 0; $i--){
//            $carbon =  Carbon::now()->subMonth($i)->startOfMonth();
//            $date = $carbon->year.'-'. $carbon->month;
//
//            $monthPtr = $carbon->month;
//
//            if (!isset($year[$date])){
//                $year[$date] = [
//                    'date' => $date
//                ];
//            }
//
//            if (!isset($months[$monthPtr])){
//                $year[$date]['month'] = 0;
//            }else{
//                $year[$date]['month'] = $months[$monthPtr]['total'];
//            }
//
//            if (!isset($orders[$monthPtr])){
//                $year[$date]['order'] = 0;
//            }else{
//                $year[$date]['order'] = $orders[$monthPtr]['total'];
//            }
//
//        }
//
//
//        $grid = new Grid(new User());
//
//        $filter = new Filter($grid->model());
//
//        $filter->column(1/3, function ($filter) {
//            $filter->like( 'name',__('Name'));
//            $filter->equal('sex','性别')->select(CommonStatement::sex);
//
//        });
//
//        $filter->column(1/3, function ($filter) {
//
//            $filter->between('created_at',__('Created at'))->datetime();
//        });

        return $content
            ->header('平台统计')
            ->body(new Box('平台统计', view('admin.index.index',[

            ])));
    }

    /**
     * 编辑器图片上传
     * Created by xiaosong
     * E-mail:4155433@gmail.com
     * @param Request $request
     * @return array
     */
    public function upload(Request $request)
    {
        $urls = [];

        foreach ($request->file() as $file) {
            $urls[] = Storage::url($file->store('images'));
        }

        return [
            "errno" => 0,
            "data"  => $urls,
        ];
    }
}